package com.itheima.dubbo.mysql.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.commons.pojo.UserInfo;
import com.itheima.dubbo.interfaces.UserInfoApi;
import com.itheima.dubbo.mysql.mapper.UserInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

public class UserInFoImpl implements UserInfoApi {
    @Autowired
    private UserInfoMapper userInfoMapper;

    //添加userinfo表的数据
    @Override
    public void addUserInfo(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);

        System.out.println("day02 userinfo添加成功");

    }

    //添加图片地址到userinfo表格中
    @Override
    public void updateLogo(Long uid, String pictureURL) {
        userInfoMapper.update(UserInfo.builder().logo(pictureURL).build(),
                Wrappers.lambdaQuery(UserInfo.class)
                        .eq(UserInfo::getUserId, uid));
        System.out.println("day02 图片地址添加成功进userinfo");
    }

 //根据分数最高的id找到用户信息
    @Override
    public UserInfo findUserInfoByUserId(Long uid) {
        UserInfo userInfo = userInfoMapper.selectById(uid);
        return userInfo;
    }

    @Override
    public Map<Long, UserInfo> findUserInfoListByUserIds(UserInfo userInfo, List<Long> userIds) {
// 查询条件  没有传递过来
        List<UserInfo> userInfos = userInfoMapper.selectList(Wrappers.lambdaQuery(UserInfo.class)
                .in(UserInfo::getUserId, userIds) );
        // 性别的第一个问题  man woman  1 2 在Service层进行转换
        // 如果是个空  APP没有传递
        // Map  "gender":"man"
        // map.get("gender")  ==  man
        // userInfo.getSex 1 , 2
        Map<Long, UserInfo> infoMap = CollUtil.fieldValueMap(userInfos, "userId");
        return infoMap;

    }
}
